package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemNeeddetailSignupMessageBinding;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.sgb.kjwl.utils.PointUtils;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class NeedDetailSignuoAdpater extends BaseRecycleAdapter<NeedDetailSignUpEntity> {

    private boolean checkuser = false;//是否自己发布
    private boolean isList  = false;//是否是更多列表
    private boolean offerStatus = false;//需求是否结束
    private boolean ishome  = false;//是否前端首页进入的列表

    public NeedDetailSignuoAdpater(Context context) {
        super(context, R.layout.item_needdetail_signup_message, new ArrayList<>());
    }


    public void setdata(List<NeedDetailSignUpEntity> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void adddata(List<NeedDetailSignUpEntity> datas){
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }


    public void setCheckuser(boolean checkuser){
        this.checkuser = checkuser;
        notifyDataSetChanged();
    }

    public void setList(boolean islist){
        this.isList = islist;
        notifyDataSetChanged();
    }

    public void setIshome(boolean ishome){
        this.ishome = ishome;
        notifyDataSetChanged();
    }


    public void setOfferStatus(boolean offerStatus){
        this.offerStatus = offerStatus;
        notifyDataSetChanged();
    }




    @Override
    public void convert(ItemViewHolder holder, NeedDetailSignUpEntity needDetailSignUpEntity) {

        if(holder.getmBinding() instanceof ItemNeeddetailSignupMessageBinding){
            ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).setSignupmodel(needDetailSignUpEntity);
            ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).linLink.setVisibility(isList? View.VISIBLE:View.GONE);
            ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).linTime.setVisibility(isList? View.VISIBLE:View.GONE);
            if(checkuser&&!ishome) {
                if(!offerStatus){//报价中
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvChoice.setVisibility(View.VISIBLE);
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvYzx.setVisibility(View.GONE);
                }else//已结束
                if(needDetailSignUpEntity.getStatus()==2){//中选
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvChoice.setVisibility(View.GONE);
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvYzx.setVisibility(View.VISIBLE);
                }else{
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvChoice.setVisibility(View.GONE);
                    ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvYzx.setVisibility(View.INVISIBLE);
                }
            }else{
                ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvChoice.setVisibility(View.INVISIBLE);
                ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvYzx.setVisibility(View.INVISIBLE);
            }
            if(needDetailSignUpEntity.getPrice()>-1) {
                ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).price.setText("￥"+ PointUtils.price(needDetailSignUpEntity.getPrice()));
            }else{
                ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).price.setText("议价");
            }
            switch (needDetailSignUpEntity.getUsertype()){
                case 1:

                    showCompanyData(((ItemNeeddetailSignupMessageBinding)holder.getmBinding()),needDetailSignUpEntity);
                    break;
                case 2:
                    ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).imageType.setImageResource(R.mipmap.image_needdetail_signup_team);
                    showTeamAndUserData(((ItemNeeddetailSignupMessageBinding)holder.getmBinding()),needDetailSignUpEntity);
                    break;
                default:
                    ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).imageType.setImageResource(R.mipmap.image_needdetail_signup_user);
                    showTeamAndUserData(((ItemNeeddetailSignupMessageBinding)holder.getmBinding()),needDetailSignUpEntity);
                    break;
            }


            if(checkuser) {
                final int indexposition = holder.getIndex_position();
                ((ItemNeeddetailSignupMessageBinding) holder.getmBinding()).tvChoice.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //报价详情
                        if (onItemClick != null) {
                            onItemClick.onItemClick(datas.get(indexposition), -1);
                        }
                    }
                });
            }
            if(holder.getIndex_position()==datas.size()-1){
                ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).viewLine.setVisibility(View.INVISIBLE);
            }else{
                ((ItemNeeddetailSignupMessageBinding)holder.getmBinding()).viewLine.setVisibility(View.VISIBLE);
            }
        }

    }


    private void showCompanyData(ItemNeeddetailSignupMessageBinding binding, NeedDetailSignUpEntity needDetailSignUpEntity){
        binding.imageType.setImageResource(R.mipmap.image_needdetail_signup_company);
        if(checkuser){
            binding.tvName.setText(TextUtils.isEmpty(needDetailSignUpEntity.getName())?"/":needDetailSignUpEntity.getName());
            binding.tvLinkman.setText(TextUtils.isEmpty(needDetailSignUpEntity.getLinkman())?"/":needDetailSignUpEntity.getLinkman());
            binding.tvLinkphone.setText(TextUtils.isEmpty(needDetailSignUpEntity.getLinkphone())?"/":needDetailSignUpEntity.getLinkphone());
        }else{
            if(!TextUtils.isEmpty(needDetailSignUpEntity.getName())){
                binding.tvName.setText(StringUtil.showCompanyName(needDetailSignUpEntity.getName()));
            }else{
                binding.tvName.setText("/");
            }
            if(!TextUtils.isEmpty(needDetailSignUpEntity.getLinkman())){
                if(needDetailSignUpEntity.getLinkman().length()>1) {
                binding.tvLinkman.setText(StringUtil.goneName(needDetailSignUpEntity.getLinkman()));
                }else{
                    binding.tvLinkman.setText("*");
                }
            }else{
                binding.tvLinkman.setText("/");
            }

            if(!TextUtils.isEmpty(needDetailSignUpEntity.getLinkphone())){
                binding.tvLinkphone.setText(StringUtil.getPhoneToName(needDetailSignUpEntity.getLinkphone()));
            }else{
                binding.tvLinkphone.setText("/");
            }


        }
    }
    private void showTeamAndUserData(ItemNeeddetailSignupMessageBinding binding, NeedDetailSignUpEntity needDetailSignUpEntity){

        if(checkuser){
            binding.tvName.setText(TextUtils.isEmpty(needDetailSignUpEntity.getName())?"/":needDetailSignUpEntity.getName());
            binding.tvLinkman.setText(TextUtils.isEmpty(needDetailSignUpEntity.getLinkman())?"/":needDetailSignUpEntity.getLinkman());
            binding.tvLinkphone.setText(TextUtils.isEmpty(needDetailSignUpEntity.getLinkphone())?"/":needDetailSignUpEntity.getLinkphone());
        }else{
            if(!TextUtils.isEmpty(needDetailSignUpEntity.getName())){
                binding.tvName.setText(StringUtil.showRealName(needDetailSignUpEntity.getName()));
            }else{
                binding.tvName.setText("/");
            }
            if(!TextUtils.isEmpty(needDetailSignUpEntity.getLinkman())){
                if(needDetailSignUpEntity.getLinkman().length()>1) {
                    binding.tvLinkman.setText(StringUtil.goneName(needDetailSignUpEntity.getLinkman()));
                }else{
                    binding.tvLinkman.setText("*");
                }
            }else{
                binding.tvLinkman.setText("/");
            }

            if(!TextUtils.isEmpty(needDetailSignUpEntity.getLinkphone())){
                binding.tvLinkphone.setText(StringUtil.getPhoneToName(needDetailSignUpEntity.getLinkphone()));
            }else{
                binding.tvLinkphone.setText("/");
            }

        }
    }


}
